package DEEPERsource.DEEPERsource.source.machinelearning.features;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import deeper.Interaction;
import deeper.InteractionType;
import edu.uci.ics.jung.graph.DirectedEdge;
import edu.uci.ics.jung.graph.Vertex;
import graph.Keys;

public class DEEPERFeatureBuilder implements FeatureBuilder {
	
	Instances dataset;
	
	public DEEPERFeatureBuilder(){
		defineDatasetStructure();
	}
	
	public Instances getInstanceStructure(){
		return dataset;
	}

	public Instance getFeatureVector(Interaction i) {
		/*List<String> leftBranch = new ArrayList<String>();
		List<String> rightBranch = new ArrayList<String>();
		Vertex v = path.getLeaf1();
		Set<DirectedEdge> in = v.getInEdges();
		while(!in.isEmpty()){
			DirectedEdge e = in.iterator().next();
			leftBranch.add(e.getUserDatum(Keys.LABEL).toString());
			v = e.getSource();
			in = v.getInEdges();
		}
		
		v = path.getLeaf2();
		in = v.getInEdges();
		while(!in.isEmpty()){
			DirectedEdge e = in.iterator().next();
			rightBranch.add(e.getUserDatum(Keys.LABEL).toString());
			v = e.getSource();
			in = v.getInEdges();
		}
		
		String[] feats = new String[17];
		
		for(int j = leftBranch.size(); j>0; j--){
			feats[leftBranch.size()-j] = leftBranch.get(j-1);
		}
		for(int j = leftBranch.size(); j<6; j++){
			feats[j] = "?";
		}
		for(int j = rightBranch.size(); j>0; j--){
			feats[6+rightBranch.size()-j] = rightBranch.get(j-1);
		}
		for(int j = rightBranch.size(); j<6; j++){
			feats[j+6] = "?";
		}
		feats[12] = Integer.toString(i.parsePath.getLabelOccursInLeft("S"));
		feats[13] = Integer.toString(i.parsePath.getLabelOccursInRight("S"));
		feats[14] = Integer.toString(i.parsePath.getLeftDepth());
		feats[15] = Integer.toString(i.parsePath.getRightDepth());
		feats[16] = i.status.toString();
		
		Instance inst = new Instance(17);
		Attribute l1 = dataset.attribute("L1");
		if(feats[0].equals("?"))	inst.setValue(l1, Instance.missingValue());
		else						inst.setValue(l1, feats[0]);		
		Attribute l2 = dataset.attribute("L2");
		if(feats[1].equals("?"))	inst.setValue(l2, Instance.missingValue());
		else						inst.setValue(l2, feats[1]);
		Attribute l3 = dataset.attribute("L3");
		if(feats[2].equals("?"))	inst.setValue(l3, Instance.missingValue());
		else						inst.setValue(l3, feats[2]);
		Attribute l4 = dataset.attribute("L4");
		if(feats[3].equals("?"))	inst.setValue(l4, Instance.missingValue());
		else						inst.setValue(l4, feats[3]);
		Attribute l5 = dataset.attribute("L5");
		if(feats[4].equals("?"))	inst.setValue(l5, Instance.missingValue());
		else						inst.setValue(l5, feats[4]);
		Attribute l6 = dataset.attribute("L6");
		if(feats[5].equals("?"))	inst.setValue(l6, Instance.missingValue());
		else						inst.setValue(l6, feats[5]);
		Attribute r1 = dataset.attribute("R1");
		if(feats[6].equals("?"))	inst.setValue(r1, Instance.missingValue());
		else						inst.setValue(r1, feats[6]);
		Attribute r2 = dataset.attribute("R2");
		if(feats[7].equals("?"))	inst.setValue(r2, Instance.missingValue());
		else						inst.setValue(r2, feats[7]);
		Attribute r3 = dataset.attribute("R3");
		if(feats[8].equals("?"))	inst.setValue(r3, Instance.missingValue());
		else						inst.setValue(r3, feats[8]);
		Attribute r4 = dataset.attribute("R4");
		if(feats[9].equals("?"))	inst.setValue(r4, Instance.missingValue());
		else						inst.setValue(r4, feats[9]);
		Attribute r5 = dataset.attribute("R5");
		if(feats[10].equals("?"))	inst.setValue(r5, Instance.missingValue());
		else						inst.setValue(r5, feats[10]);
		Attribute r6 = dataset.attribute("R6");
		if(feats[11].equals("?"))	inst.setValue(r6, Instance.missingValue());
		else						inst.setValue(r6, feats[11]);
		Attribute sLeft = dataset.attribute("sLeft");
		inst.setValue(sLeft, Double.parseDouble(feats[12]));
		Attribute sRight = dataset.attribute("sRight");
		inst.setValue(sRight, Double.parseDouble(feats[13]));
		Attribute lDist = dataset.attribute("lDist");
		inst.setValue(lDist, Double.parseDouble(feats[14]));
		Attribute rDist = dataset.attribute("rDist");
		inst.setValue(rDist, Double.parseDouble(feats[15]));
		Attribute cls = dataset.attribute("class");
		inst.setValue(cls, feats[16]);
		return inst;*/
		return null;
	}
	
	public void defineDatasetStructure() {
		FastVector attributes = new FastVector(17);
		FastVector attStructure = new FastVector();
		attStructure.addElement("dep");
		attStructure.addElement("aux");
		attStructure.addElement("auxpass");
		attStructure.addElement("cop");
		attStructure.addElement("conj");
		attStructure.addElement("conj_nor");
		attStructure.addElement("conj_only");
		attStructure.addElement("conj_negcc");
		attStructure.addElement("conj_versus");
		attStructure.addElement("conj_and");
		attStructure.addElement("conj_but");
		attStructure.addElement("conj_or");
		attStructure.addElement("cc");
		attStructure.addElement("arg");
		attStructure.addElement("subj");
		attStructure.addElement("nsubj");
		attStructure.addElement("nsubjpass");
		attStructure.addElement("csubj");
		attStructure.addElement("measure");
		attStructure.addElement("comp");
		attStructure.addElement("obj");
		attStructure.addElement("dobj");
		attStructure.addElement("iobj");
		attStructure.addElement("pobj");
		attStructure.addElement("attr");
		attStructure.addElement("ccomp");
		attStructure.addElement("xcomp");
		attStructure.addElement("compl");
		attStructure.addElement("mark");
		attStructure.addElement("rel");
		attStructure.addElement("acomp");
		attStructure.addElement("agent");
		attStructure.addElement("csubjpass");
		attStructure.addElement("ref");
		attStructure.addElement("expl");
		attStructure.addElement("mod");
		attStructure.addElement("advcl");
		attStructure.addElement("purpcl");
		attStructure.addElement("tmod");
		attStructure.addElement("rcmod");
		attStructure.addElement("amod");
		attStructure.addElement("infmod");
		attStructure.addElement("partmod");
		attStructure.addElement("num");
		attStructure.addElement("number");
		attStructure.addElement("appos");
		attStructure.addElement("nn");
		attStructure.addElement("abbrev");
		attStructure.addElement("advmod");
		attStructure.addElement("neg");
		attStructure.addElement("poss");
		attStructure.addElement("possessive");
		attStructure.addElement("prt");
		attStructure.addElement("det");
		attStructure.addElement("prep");
		attStructure.addElement("prep_onto");
		attStructure.addElement("prep_involving");
		attStructure.addElement("prep_while");
		attStructure.addElement("prep_toward");
		attStructure.addElement("prep_due_to");
		attStructure.addElement("prep_based_on");
		attStructure.addElement("prep_compared_to");
		attStructure.addElement("prep_against");
		attStructure.addElement("prep_over");
		attStructure.addElement("prep_compared_with");
		attStructure.addElement("prep_including");
		attStructure.addElement("prep_concerning");
		attStructure.addElement("prep_regarding");
		attStructure.addElement("prep_whether");
		attStructure.addElement("prep_despite");
		attStructure.addElement("prep_on");
		attStructure.addElement("prep_of");
		attStructure.addElement("prep_under");
		attStructure.addElement("prep_in");
		attStructure.addElement("prep_as");
		attStructure.addElement("prep_for");
		attStructure.addElement("prep_during");
		attStructure.addElement("prep_at");
		attStructure.addElement("prep_from");
		attStructure.addElement("prep_after");
		attStructure.addElement("prep_before");
		attStructure.addElement("prep_to");
		attStructure.addElement("prep_by");
		attStructure.addElement("prep_into");
		attStructure.addElement("prep_through");
		attStructure.addElement("prep_between");
		attStructure.addElement("prep_with");
		attStructure.addElement("prep_via");
		attStructure.addElement("prep_upon");
		attStructure.addElement("prep_like");
		attStructure.addElement("prep_near");
		attStructure.addElement("prep_within");
		attStructure.addElement("prep_following");
		attStructure.addElement("prep_inside");
		attStructure.addElement("prep_than");
		attStructure.addElement("prep_beyond");
		attStructure.addElement("prep_unlike");
		attStructure.addElement("prepc_from");
		attStructure.addElement("prepc_since");
		attStructure.addElement("prepc_on");
		attStructure.addElement("prepc_at");
		attStructure.addElement("prepc_of");
		attStructure.addElement("prepc_for");
		attStructure.addElement("prepc_in");
		attStructure.addElement("prepc_by");
		attStructure.addElement("prepc_despite");
		attStructure.addElement("sdep");
		attStructure.addElement("xsubj");
		attributes.addElement(new Attribute("L1",attStructure));
		attributes.addElement(new Attribute("L2",attStructure));
		attributes.addElement(new Attribute("L3",attStructure));
		attributes.addElement(new Attribute("L4",attStructure));
		attributes.addElement(new Attribute("L5",attStructure));
		attributes.addElement(new Attribute("L6",attStructure));
		attributes.addElement(new Attribute("R1",attStructure));
		attributes.addElement(new Attribute("R2",attStructure));
		attributes.addElement(new Attribute("R3",attStructure));
		attributes.addElement(new Attribute("R4",attStructure));
		attributes.addElement(new Attribute("R5",attStructure));
		attributes.addElement(new Attribute("R6",attStructure));
		attributes.addElement(new Attribute("sLeft"));
		attributes.addElement(new Attribute("sRight"));
		attributes.addElement(new Attribute("lDist"));
		attributes.addElement(new Attribute("rDist"));		
		FastVector classValues = new FastVector();
		classValues.addElement(InteractionType.FALSE.ordinal());
		classValues.addElement(InteractionType.REAL.ordinal());
		classValues.addElement(InteractionType.UNKNOWN.ordinal());
		classValues.addElement(InteractionType.REALSTRAIGHT.ordinal());
		classValues.addElement(InteractionType.REALINVERSE.ordinal());
		attributes.addElement(new Attribute("class", classValues));
		dataset = new Instances("", attributes, 100);
		dataset.setClassIndex(dataset.numAttributes()-1);
	}
}
